/**
 * @Author: quanju.wei@ys100.com
 */
(function () {
    var meta = document.createElement('meta');
    meta.name = 'renderer';
    meta.content = 'webkit';
    document.querySelector('head').appendChild(meta);
})();
(function setTemplate() {
    var start = new Date().getTime();
    $('#write').hide();
    $('body').append('<div class="d-flex justify-content-around " id="b_loading"><div class="spinner-grow text-success" role="status"><span class="sr-only">Loading...</span></div></div>');

    $(document).ready(ready);

    (function setHeadNav() {
        /**
         * 重置Typro
         */
        $('style').remove();
        $('<style>html,body{height: 100%;width:100%}.doc-nav{background: #f8f8f8;border-right: 1px solid #eee;} #doc-container,.flex-xl-nowrap{height: calc( 100vh - 56px )}.doc-nav{height: inherit;overflow-y: auto}.doc-content{width: 100%;height: inherit;overflow: auto;background: #fff;}.nav-link{color: #e0e0e0;transition: color .5s;}.nav-link:hover,.nav-link.active{color:#fff;font-weight: bolder;}#b_loading{height: 100%;background: rgba(0,0,0,.01); transition: background 300ms ease;}#b_loading.d-none{height: 0;}#b_loading.d-none>.spinner-grow{display: none!important;} #b_loading>.spinner-grow{position: fixed;top: 45%;}</style>').appendTo('head');
        $(
            '<link rel="stylesheet" href="lib/bootstrap.min.css">' +
            '<link rel="stylesheet" href="lib/typro.css">' +
            '<script src="lib/bootstrap.min.js"></script>').appendTo('head');

    })();

    function ready() {
        /**
         * 文档配置
         * @type {{doc: *[], title: string}}
         */
        var config = {
            title:'项目接口文档',
            doc:[
                {name:'接口通用说明',file:'index.html'},
                {name:'公共接口文档',file:'common.html'},
                {name:'api/v1工程接口文档',file:'apiv1.html'},
                {name:'admin工程接口文档',file:'admin.html'},
                {name:'websocket工程接口文档',file:'websocket.html'},
                {name:'帮助',file:'help.html'},
            ]
        };

        $('<header class="navbar navbar-expand bg-dark" id="doc-head" style="display: none;height: 0;">\n' +
            '    <ul class="nav" id="header-nav"></ul>\n' +
            '</header>\n' +
            '<section class="container-fluid" style="display: none;height: 0;" id="doc-container">\n' +
            '    <div class="row flex-xl-nowrap">\n' +
            '        <div class="col-12 col-md-3 col-xl-2 doc-nav" style="display: none;height: 0;">\n' +
            '            <!--目录区域 start-->\n' +
            '            <!--目录区域 end-->\n' +
            '        </div>\n' +
            '        <div class="col-12 col-md-9 col-xl-10 doc-content" style="display: none;height: 0;">\n' +
            '            <!--内容区域 start-->\n' +
            '            <!--内容区域 end-->\n' +
            '        </div>\n' +
            '    </div>\n' +
            '</section>').appendTo('body');

        var $nav,x ,actionDoc,doc;

        $nav = $('#header-nav');
        doc = config.doc;

        actionDoc = getActionFile(doc);

        $('title').text(config.title+(actionDoc ? " - "+actionDoc.name : ''));
        $nav.empty().append($('<a class="navbar-brand text-white" href="index.html">' + config.title + '</a>\n'));

        for(x in doc){
            var item = doc[x];
            $nav.append(
                '        <li class="nav-item">\n' +
                '            <a class="nav-link' + (actionDoc && item.file === actionDoc.file ? ' active' :'') + '" href="' + item.file + '">' + item.name + '</a>\n' +
                '        </li>\n')
        }


        $('.md-toc').appendTo('.doc-nav');
        $('#write').removeAttr('class').appendTo('.doc-content');
        $('body').removeAttr('class');
        var cost = new Date().getTime() - start;

        setTimeout(function () {
            console.log('run UI init!');
            $('#b_loading').addClass('d-none');
            $('#write').show();
            $('#doc-head').removeAttr('style');
            $("#doc-container").removeAttr('style');
            $('.doc-nav').removeAttr('style');
            $('.doc-content').removeAttr('style');
        },cost>600 ? 0: 600-cost);
    }

    function getActionFile(doc) {
        var x;
        for(x in doc){
            if(new RegExp(doc[x].file).test(location.pathname)){
                return doc[x];
            }
        }
    }
})();
